package com.itheima.utils;

import com.itheima.domain.SysLog;
import com.itheima.domain.UserInfo;
import com.itheima.utils.UidUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class GetSysLogUtil {
    public static SysLog getSysLog(HttpServletRequest req, HttpServletResponse resp) {
        SysLog sysLog = new SysLog();
        //获取资源路径
        String url = req.getRequestURL().toString();
        //抽取方法名
        String uri = req.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        //获取ip
        String ip = req.getRemoteAddr();
        //获得用户名
        UserInfo user = (UserInfo) req.getSession().getAttribute("user");
        if (user != null) {
            String username = user.getUsername();
            String s = String.valueOf(username);
            //获取当前时间
            Date date = new Date();

            String id = UidUtil.getUid();
            sysLog.setId(id);
            sysLog.setVisitTime(date);
            sysLog.setUsername(s);
            sysLog.setIp(ip);
            sysLog.setMethod(methodName);
            sysLog.setUrl(url);

            //sysLog.setExecutionTime(time);

            return sysLog;
        }
        return null;
    }
}
